import java.util.*;
import java.lang.Integer;

class phone {
  private static boolean DEBUG = true;
  
  private static Map<String, Integer> mapper = new HashMap<String, Integer>();
  
  private static void initMapper() {
    mapper.put("A", 2);
    mapper.put("B", 2);
    mapper.put("C", 2);
    mapper.put("D", 3);
    mapper.put("E", 3);
    mapper.put("F", 3);
    mapper.put("G", 4);
    mapper.put("H", 4);
    mapper.put("I", 4);
    mapper.put("J", 5);
    mapper.put("K", 5);
    mapper.put("L", 5);
    mapper.put("M", 6);
    mapper.put("N", 6);
    mapper.put("O", 6);
    mapper.put("P", 7);
    mapper.put("R", 7);
    mapper.put("S", 7);
    mapper.put("T", 8);
    mapper.put("U", 8);
    mapper.put("V", 8);
    mapper.put("W", 9);
    mapper.put("X", 9);
    mapper.put("Y", 9);
  }
  
  public static boolean isStdForm(String phoneNumber) {
    // check length matches
    int phoneLen = phoneNumber.length();
    if (phoneLen == 7) {
      return phoneNumber.matches("^[0-9A-Z].*");
    } else if (phoneLen == 8) {
      return (phoneNumber.charAt(3) == '-') && (phoneNumber.substring(0,3).matches("^[0-9A-Z].*")) && (phoneNumber.substring(4,8).matches("^[0-9A-Z].*"));
    }
    return false;
  }
  
  public static String convertToStdForm(String phoneNumber) {
    int phoneLen = phoneNumber.length();
    int stdLen = 8;
    StringBuilder stdForm = new StringBuilder();
    boolean dashAdded = false;

    for (int i = 0; i < phoneLen && stdLen > 0; i++) {
      char cur = phoneNumber.charAt(i);
      if (Character.isDigit(cur)) {
        stdForm.append(cur);
        stdLen--;
      } else if (mapper.containsKey(String.valueOf(cur))) {
        stdForm.append(mapper.get(String.valueOf(cur)));
        stdLen--;
      }
      if (stdLen == 5 && !dashAdded) {
        stdForm.append('-');
        dashAdded = true;
      }
    }
    
    return stdForm.toString();
  }
  
  public static void run(Scanner scanner) {
    int numPhoneNumbers = Integer.parseInt(scanner.nextLine());
    Map<String, Integer> phoneBook = new HashMap<String, Integer>();
    
    for (int i = 0; i < numPhoneNumbers; i++) {
      String curLine = scanner.nextLine();
      if (isStdForm(curLine)) {
        String stdLine = convertToStdForm(curLine);
        if (DEBUG) {
          System.out.println("Convert " + curLine + " into " + stdLine);
        }
        if (phoneBook.containsKey(stdLine)) {
          phoneBook.put(stdLine, phoneBook.get(stdLine) + 1);
        } else {
          phoneBook.put(stdLine, 1);
        }
      } else {
        System.out.println("Not in the standard form");
      }
    }
    
    if (DEBUG) {
      System.out.println("Phone Book size is " + phoneBook.size());
    }

    TreeSet<String> keys = new TreeSet<String>(phoneBook.keySet());
    for (Iterator i = keys.iterator(); i.hasNext();) {
      String key = (String) i.next();
      System.out.println(key + " " + phoneBook.get(key));
    }
  }
  
  public static void main(String[] args) {
    Scanner scanner = new Scanner(System.in);
    initMapper();
    int cases = Integer.parseInt(scanner.nextLine());
    for(int i = 0; i < cases; i++) {
      run(scanner);
    }

    if (scanner != null) {
      scanner.close();
    }
  }
}